{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sqlite3 # 导入sqlite3库"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建数据库和table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  连接到一个数据库名为 data_base_name 的数据库,如果存在则直接连接,如果不存在则创建\n",
    "data_base=sqlite3.connect('data_base_name')\n",
    "# 设置数据库光标,你之后所有对数据库进行的操作都是通过光标来执行的\n",
    "cursor=data_base.cursor()\n",
    "# 创建一个表名为 table_name 的数据库表,如果这个表不存在的话\n",
    "# 后面括号里面的内容为这个表的属性,属性与属性之间用  ,  隔开,属性名与属性类型之间用 空格  隔开,如果不写类型的话,默认       为text类型\n",
    "cursor.execute('create table if not exists table_name(name text,age int,info text)')\n",
    "# 数据库的提交,对数据进行增删改后都需要进行数据库的提交\n",
    "data_base.commit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 增加数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor.execute('insert into table_name (name,age,info) VALUES (\"python\",29,\"最接近人工智能的计算机语言\")')\n",
    "data_base.commit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('python', 29, '最接近人工智能的计算机语言'), ('python', 29, '最接近人工智能的计算机语言')]\n"
     ]
    }
   ],
   "source": [
    "cursor.execute('select * from table_name')\n",
    "result=cursor.fetchall()\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 修改数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor.execute('update table_name set name=\"C++\",age=33 WHERE name=\"python\"')\n",
    "cursor.execute('update table_name set name=\"C++\",age=33 WHERE age=29')\n",
    "data_base.commit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('C++', 33, '最接近人工智能的计算机语言'), ('C++', 33, '最接近人工智能的计算机语言')]\n"
     ]
    }
   ],
   "source": [
    "cursor.execute('select * from table_name')\n",
    "result=cursor.fetchall()\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 删除数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor.execute('DELETE FROM table_name WHERE name=\"C++\"')\n",
    "data_base.commit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "cursor.execute('select * from table_name')\n",
    "result=cursor.fetchall()\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 查询结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor.execute('insert into table_name (name,age,info) VALUES (\"python\",29,\"最接近人工智能的计算机语言\")')\n",
    "data_base.commit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('python', 29, '最接近人工智能的计算机语言')]\n"
     ]
    }
   ],
   "source": [
    "cursor.execute('select * from table_name')\n",
    "result=cursor.fetchall()\n",
    "print(result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
